<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>canvas</title>
  <style>
    body {
      margin: 0;
    }

    canvas {
      position: absolute;
      left: 0;
    }
  </style>
</head>

<body>
  <img src="./images/timg2.jpeg" />
  <canvas id="canvas" width="600" height="338">
    你的浏览器太low了~
  </canvas>
  <script>

    window.onload = function () {
      var canvas = document.getElementById('canvas')
      console.info();
      if (canvas.getContext) {
        var ctx = canvas.getContext('2d');
        ctx.beginPath();
        // 1、画灰色矩形框挡住图片
        ctx.fillStyle = '#CCC';
        ctx.fillRect(0, 0, canvas.width, canvas.height)

        // 后写的是源图像
        ctx.globalCompositeOperation = 'destination-out';
        ctx.lineCap = 'round'
        ctx.lineWidth = 35;

        canvas.onmousedown = function (e) {
          var ev = e || window.event; // event兼容
          // 获取鼠标按下位置
          var x = ev.clientX;
          var y = ev.clientY;

          ctx.moveTo(x, y);

          canvas.onmousemove = function (e) {
            var ev = e || window.event;
            var x = ev.clientX;
            var y = ev.clientY;
            ctx.lineTo(x, y);
            ctx.stroke()
          }

          canvas.onmouseup = function () {
            canvas.onmousemove = null
          }
        }
      } else {
        alert('你的浏览器太low了~')
      }
    }
  </script>

</body>

</html>